package com.bkgy.modular.tbpaopiantensiondata.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.tbpaopiantensiondata.dao.TbPaopianTensionDataDao;
import com.bkgy.modular.tbpaopiantensiondata.entity.JTbPaopianTensionData;
import com.bkgy.modular.tbpaopiantensiondata.entity.TbPaopianTensionData;
import com.bkgy.modular.tbpaopiantensiondata.service.TbPaopianTensionDataService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 连退跑偏张力表接口具体实现类
* @author  生成器生成
* @date 2022-08-10 13:23:41
*/
@Service
public class TbPaopianTensionDataServiceImpl implements TbPaopianTensionDataService {
	
	@Autowired
	private TbPaopianTensionDataDao tbPaopianTensionDataDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbPaopianTensionData(String data,Long userId,String sname){
		try{
			JTbPaopianTensionData jtbPaopianTensionData = JSON.parseObject(data,JTbPaopianTensionData.class);
    		TbPaopianTensionData tbPaopianTensionData = jtbPaopianTensionData.getTbPaopianTensionData();
    		CodiUtil.newRecord(userId,sname,tbPaopianTensionData);
            tbPaopianTensionDataDao.insertDataTbPaopianTensionData(tbPaopianTensionData);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbPaopianTensionDataOne(Long indocno){
		try {
            tbPaopianTensionDataDao.deleteDataTbPaopianTensionDataOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbPaopianTensionDataMany(String str_id) {
        try {
        	String sql = "delete tb_paopian_tension_data where indocno in(" + str_id +")";
            tbPaopianTensionDataDao.deleteDataTbPaopianTensionDataMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbPaopianTensionData(String data,Long userId,String sname){
		try {
			JTbPaopianTensionData jtbPaopianTensionData = JSON.parseObject(data,JTbPaopianTensionData.class);
    		TbPaopianTensionData tbPaopianTensionData = jtbPaopianTensionData.getTbPaopianTensionData();
        	CodiUtil.editRecord(userId,sname,tbPaopianTensionData);
            tbPaopianTensionDataDao.updateDataTbPaopianTensionData(tbPaopianTensionData);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbPaopianTensionDataByPage(String data) {
        try {
        	JTbPaopianTensionData jtbPaopianTensionData = JSON.parseObject(data, JTbPaopianTensionData.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbPaopianTensionData.getPageIndex();
        	Integer pageSize = jtbPaopianTensionData.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbPaopianTensionData.getCondition()){
    			jsonObject  = JSON.parseObject(jtbPaopianTensionData.getCondition().toString());
    		}
			String extid = null;  //出口卷号
			if (!StringUtils.isEmpty(jsonObject.get("extid"))) {
				extid = jsonObject.get("extid").toString();
			}
    
    		List<TbPaopianTensionData> list = tbPaopianTensionDataDao.findDataTbPaopianTensionDataByPage((pageIndex-1)*pageSize, pageIndex*pageSize,extid);
    		Integer count = tbPaopianTensionDataDao.findDataTbPaopianTensionDataByPageSize(extid);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbPaopianTensionDataByIndocno(String data) {
        try {
        	JTbPaopianTensionData jtbPaopianTensionData = JSON.parseObject(data, JTbPaopianTensionData.class); 
        	Long indocno = jtbPaopianTensionData.getIndocno();
        	
    		TbPaopianTensionData tbPaopianTensionData = tbPaopianTensionDataDao.findDataTbPaopianTensionDataByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbPaopianTensionData);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbPaopianTensionData> findDataTbPaopianTensionData(){
		List<TbPaopianTensionData> list = tbPaopianTensionDataDao.findDataTbPaopianTensionData();
		return list;
	};
}
